﻿using System.Collections.Generic;
using System.Linq;

namespace PersonaLibrary.Commands.EditCommands
{
    public class DeleteDepartment : CommandBase
    {
        public override bool CanExecute(object parameter)
        {
            return parameter is int;
        }

        public override void Execute(object parameter)
        {
            var depRep = (DataModel.DepartmentRepository)DataModel.DataBase.GetRepository<DataModel.DepartmentRepository, DataModel.DepartmentEntity>();
            var dep = depRep.GetEntity((int)parameter);
            var conRep = (DataModel.ConnectionRepository)DataModel.DataBase.GetRepository<DataModel.ConnectionRepository, DataModel.ConnectionEntity>();
            var ids = conRep.GetAllConnectionsWithDepartment((int)parameter).Select(x => x.Id).ToList();
            foreach (int id in ids)
                conRep.Delete(id);
            depRep.Delete((int)parameter);
        }
    }
}
